﻿Imports System.Windows.Media.Animation
Imports System.Windows.Threading

Partial Public Class AlertWindow

    Private FadeInAnimation As DoubleAnimation
    Private FadeOutAnimation As DoubleAnimation

    Private ActiveTimer As DispatcherTimer
    'Private Shared AlertCount As Integer

    Private Shared WindowPosition As New List(Of Boolean)
    Private WindowPositionIndex As Integer
    Private Shared LockObject As New Object

    Private IsClosing As Boolean

    Private Uri As Uri

    Public Sub New(ByVal context As AlertWindowDataContext)
        Me.InitializeComponent()
        Me.DataContext = context

        Me.Title = context.Title
        Me.Uri = context.Uri

        Dim workAreaRectangle = System.Windows.SystemParameters.WorkArea
        SyncLock LockObject

            Dim existPosition As Boolean
            For i = 0 To WindowPosition.Count - 1
                If WindowPosition(i) = False Then
                    WindowPosition(i) = True
                    WindowPositionIndex = i
                    existPosition = True
                    Exit For
                End If
            Next

            If Not existPosition Then
                WindowPosition.Add(True)
                WindowPositionIndex = WindowPosition.Count - 1
            End If

        End SyncLock
        Dim alertCount = WindowPositionIndex + 1

        Dim maxVerticalCount = Math.Floor((workAreaRectangle.Bottom - workAreaRectangle.Top) / Me.Height)

        Me.Left = workAreaRectangle.Right - Me.Width * (1 + Math.Floor((AlertCount - 1) / maxVerticalCount))
        Me.Top = workAreaRectangle.Bottom - Me.Height * (1 + (AlertCount - 1) Mod maxVerticalCount)

        FadeInAnimation = New DoubleAnimation()
        FadeInAnimation.From = 0
        FadeInAnimation.To = 0.9
        FadeInAnimation.Duration = New Duration(TimeSpan.Parse("0:0:0.5"))

        FadeOutAnimation = New DoubleAnimation()
        FadeOutAnimation.To = 0
        FadeOutAnimation.Duration = New Duration(TimeSpan.Parse("0:0:0.5"))

        AddHandler Me.Loaded, New RoutedEventHandler(AddressOf AlertWindow_Loaded)
    End Sub


    Private Sub AlertWindow_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)


        Me.Topmost = True
        Me.Visibility = Windows.Visibility.Visible

        AddHandler FadeInAnimation.Completed, New EventHandler(AddressOf FadeInCompleted)
        Me.BeginAnimation(AlertWindow.OpacityProperty, FadeInAnimation)

    End Sub

    Private Sub FadeInCompleted(ByVal sender As Object, ByVal e As EventArgs)
        ActiveTimer = New DispatcherTimer()
        ActiveTimer.Interval = TimeSpan.Parse("0:0:8")

        AddHandler ActiveTimer.Tick, AddressOf FadeOut
        ActiveTimer.Start()
    End Sub


    Private Sub FadeOutCompleted(ByVal sender As Object, ByVal e As EventArgs)
        Me.Close()
    End Sub

    Private Sub FadeOut(ByVal sender As Object, ByVal e As EventArgs)

        IsClosing = True
        AddHandler FadeOutAnimation.Completed, AddressOf FadeOutCompleted
        Me.BeginAnimation(AlertWindow.OpacityProperty, FadeOutAnimation, HandoffBehavior.SnapshotAndReplace)

    End Sub


    Private Sub DesktopAlertBase_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Closed
        SyncLock LockObject
            WindowPosition(WindowPositionIndex) = False
        End SyncLock
    End Sub

    Private Sub AlertWindow_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
        If ActiveTimer IsNot Nothing Then
            ActiveTimer.Stop()
        End If
    End Sub

    Private Sub AlertWindow_MouseEnter(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Me.MouseEnter
        If Not IsClosing AndAlso ActiveTimer IsNot Nothing Then
            ActiveTimer.Stop()
        End If
    End Sub

    Private Sub AlertWindow_MouseLeave(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Me.MouseLeave
        If Not IsClosing AndAlso ActiveTimer IsNot Nothing Then
            ActiveTimer.Start()
        End If
    End Sub

    Private Sub MenuItem_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Try
            Clipboard.SetText(Me.Uri.ToString)
        Catch ex As ArgumentNullException
            ' Ignore
        End Try
    End Sub

    Private Sub OpenUri(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        BrowserManager.OpenUri(Me.Uri)
        ActiveTimer.Stop()
        FadeOut(Me, New EventArgs)
    End Sub

    Private Sub CloseButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Me.Close()
    End Sub

    Private Sub Window_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        If e.LeftButton = MouseButtonState.Pressed Then
            DragMove()
        End If
    End Sub

End Class
